package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.CopymapMapper;
import com.xgame.server.db.model.Copymap;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class CopymapCsTool {
    @Resource
    private CopymapMapper copymapMapper;

    //插入经验副本
    public Integer insertCopyMap(Game.CopyMapCS data) throws Exception {
        Copymap map = new Copymap();
        map.setAccount(data.getAccount());
        map.setPlayerId(data.getPlayerID());
        map.setExpTimes(data.getExpTimes());

        String lst = "";
        List<Integer> complst = data.getCompletedListList();
        int length = complst.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + complst.get(i);
            } else {
                lst = lst + complst.get(i) + "^";
            }
        }

        map.setCompletedList(lst);
        map.setCurrentMapid(data.getCurrentMapId());
        map.setBuyTimes(data.getBuyTimes());

        return copymapMapper.insert(map);
    }

    //更新玩家记录
    public Integer updateCopyMap(Game.CopyMapCS data) throws Exception {
        EntityWrapper<Copymap> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", data.getAccount());

        Copymap map = new Copymap();
        map.setAccount(data.getAccount());
        map.setPlayerId(data.getPlayerID());
        map.setExpTimes(data.getExpTimes());

        String lst = "";
        List<Integer> complst = data.getCompletedListList();
        int length = complst.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + complst.get(i);
            } else {
                lst = lst + complst.get(i) + "^";
            }
        }

        map.setCompletedList(lst);
        map.setCurrentMapid(data.getCurrentMapId());
        map.setBuyTimes(data.getBuyTimes());

        return copymapMapper.update(map, userWrapper);
    }

    //删除记录
    public Integer deleteCopyMap(Game.CopyMapCS data) throws Exception {
        EntityWrapper<Copymap> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", data.getAccount());

        return copymapMapper.delete(userWrapper);
    }

    //查询记录
    public List<Copymap> selectCopyMap(String account) throws Exception {
        EntityWrapper<Copymap> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return copymapMapper.selectList(userWrapper);
    }
}
